package tech.aistar.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tech.aistar.entity.Com;
import tech.aistar.entity.Post;
import tech.aistar.entity.UserInfo;
import tech.aistar.mapper.PostMapper;
import tech.aistar.mapper.UserInfoMapper;
import tech.aistar.mapper.UserLoginMapper;
import tech.aistar.service.IUserInfoService;
import org.springframework.data.redis.core.*;


import java.sql.Timestamp;
import java.util.List;

@Service
@Transactional
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserLoginMapper userLoginMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private PostMapper postMapper;
    @Override
    public UserInfo findById(Integer Id) {
        UserInfo userinfo = userInfoMapper.findById(Id);
        return userinfo;
    }

    @Override
    public UserInfo findByUid(String uId) {
        UserInfo userInfo = userInfoMapper.findByUid(uId);
        return userInfo;
    }

    @Override

    public void editUserInfo(String uName, Timestamp uBirthday, Integer uSex, String Province, String City, String District, String uStatement, String picfilename, Integer id) {
        userInfoMapper.editUserInfo(uName,uBirthday,uSex,Province,City,District,uStatement,picfilename,id);
        //        System.out.println(10/0);
        userLoginMapper.editUsername(uName,id);

    }


    @Override
    public void addEmail(String uEmail, Integer id) {
        userInfoMapper.addEmail(uEmail,id);
    }

    @Override
    public void addPhone(String uPhone, Integer id) {
        userInfoMapper.addPhone(uPhone,id);
    }

    @Override
    public void delView(Integer id) {
        userInfoMapper.delView(id);
    }

    @Override
    public void addUserInfo(Integer id, String uId, String uName) {
        userInfoMapper.addUserInfo(id,uId,uName);
    }

    @Override
    public void calPost(Integer id, Integer postNum) {
        userInfoMapper.calPost(id,postNum);
    }

    @Override
    public void calFans(Integer id, Integer fansNum) {
        userInfoMapper.calFans(id,fansNum);
    }

    @Override
    public void calFouc(Integer id, Integer foucNum) {
        userInfoMapper.calFouc(id,foucNum);
    }

    @Override
    public void calLike(Integer id, Integer likeNum) {
        userInfoMapper.calLike(id,likeNum);
    }

    @Override
    public void calMyzan(Integer id, Integer myzanNum) {
        userInfoMapper.calMyzan(id,myzanNum);
    }

    @Override
    public void calComment(Integer id, Integer commentNum) {
        userInfoMapper.calComment(id,commentNum);
    }

    @Override
    public void addView(Integer id, Integer viewNum) {
        userInfoMapper.calComment(id,viewNum);
    }

    @Override
    public List<String> findPhones() {
        List<String> phones = userInfoMapper.findPhones();
        return phones;
    }

    @Override
    public List<String> findEmail() {
        List<String> email = userInfoMapper.findEmail();
        return email;
    }

    @Override
    public List<UserInfo> findAll() {
        //放入redis中
        //清空
        while(redisTemplate.opsForList().size("userinfo")>0){
            redisTemplate.opsForList().leftPop("userinfo");
        }
        //放
        redisTemplate.opsForList().rightPushAll("userinfo",userInfoMapper.findAll());
        return userInfoMapper.findAll();
    }

    @Override
    public UserInfo getinfo(Integer id) {
        //取
        redisTemplate.opsForValue().set("id",userInfoMapper.getinfo(id));
        //用的时候redisTemplate.opsForValue().get("id");
        return userInfoMapper.getinfo(id);
    }

    @Override
    public List<Post> findPostAll() {
        //放入redis中
        //清空
        while(redisTemplate.opsForList().size("post")>0){
            redisTemplate.opsForList().leftPop("post");
        }
        //放
        redisTemplate.opsForList().rightPushAll("post",postMapper.findAllPost());
        return postMapper.findAllPost();
    }


    @Override
    public List<Com> findAllByUserId(Integer userId) {
        List<Com> comlist = userInfoMapper.findAllByUserId(userId);
        return comlist;
    }

    @Override
    public List<Post> findByThemeId(Integer ThemeId) {
        List<Post> list = postMapper.findPostByTheme(ThemeId);
        return list;
    }

    @Override
    public void updatePost(Integer pointNum, Integer postId) {
        userInfoMapper.updatePost(pointNum, postId);
    }
}
